package com.cskaoyan.controller.goodsController;

import com.cskaoyan.model.bean.Comment;
import com.cskaoyan.model.bean.Goods;
import com.cskaoyan.model.bean.Storage;
import com.cskaoyan.model.bo.goods.GoodsAddBo;
import com.cskaoyan.model.bo.goods.GoodsUpdateBo;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.goods.*;
import com.cskaoyan.service.goods.GoodsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoProperties;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @RequestMapping("admin/goods/list")
    public BaseRespVo list(Integer page, Integer limit, String sort, String order) {
        GoodsVo goodsVo = goodsService.list(page, limit, sort, order);
        return BaseRespVo.ok(goodsVo);
    }

    @RequestMapping(value = "admin/goods/list", params = {"page","limit", "goodsSn","name","sort","order"})
    public BaseRespVo goodsSearch(Integer page, Integer limit, String goodsSn, String name, String sort, String order) {
        GoodsVo goodsVo = goodsService.goodsSearch(page, limit, goodsSn, name, sort, order);
        return BaseRespVo.ok(goodsVo);

    }

    @RequestMapping("admin/goods/catAndBrand")
    public BaseRespVo catAndBrandChose() {

        CatAndBrandVo catAndBrandVo = goodsService.catAndBrandSelect();
        return BaseRespVo.ok(catAndBrandVo);

    }



    @PostMapping("/admin/goods/create")
    public BaseRespVo goodsAdd(@RequestBody GoodsAddBo goodsAddBo)  {

        goodsService.goodsAdd(goodsAddBo);


        return BaseRespVo.ok();

    }


    @RequestMapping("admin/goods/detail")
    public BaseRespVo detail(@Param("id") Integer id) {

        DetailVo detail = goodsService.detail(id);
        return BaseRespVo.ok(detail);
    }

    @PostMapping("admin/goods/update")
    public BaseRespVo goodsUpdate(@RequestBody GoodsUpdateBo goodsUpdateBo) {
        goodsService.goodsUpdate(goodsUpdateBo);//注意参数验证
//        if (code != 1) {
//            return BaseRespVo.error(400, "参数错误");
//        }
        return BaseRespVo.ok();
    }
    @PostMapping("admin/goods/delete")
    public BaseRespVo goodsDelete(@RequestBody Goods goods) {
        int ret = goodsService.goodsDelete(goods);
        if (ret == 0) {
            return BaseRespVo.error(400, "chuowu");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("admin/comment/list")
    public BaseRespVo commentList(Integer page, Integer limit, String sort, String order) {
        CommentVo commentVo = goodsService.commentList(page, limit, sort, order);
        return BaseRespVo.ok(commentVo);
    }


    @PostMapping("admin/comment/delete")
    public BaseRespVo commentDel(@RequestBody Comment comment) {
        int ret = goodsService.commentDel(comment);
        if (ret == 0) {
            return BaseRespVo.error(400, "failed");
        }
        return BaseRespVo.ok();
    }

}


